package com.dfbz.demo;

/**
 * @author lscl
 * @version 1.0
 * @intro:
 */
public class Demo02_多条线程的通信 {
    public static void main(String[] args) {
        new Thread().start();

        Printer printer = new Printer();

        new Thread() {
            @Override
            public void run() {
                try {
                    printer.print1();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }.start();

        new Thread() {
            @Override
            public void run() {
                try {
                    printer.print2();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }.start();


        new Thread() {
            @Override
            public void run() {
                try {
                    printer.print3();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }.start();

    }
}

class Printer {
    int count = 1;

    public void print1() throws InterruptedException {
        for (int i = 0; i < 100; i++) {
            synchronized (Object.class) {
                while (count == 1) {
                    Thread.sleep(10);
                    System.out.print("犯");
                    System.out.print("我");
                    System.out.print("中");
                    System.out.print("华");
                    System.out.print("者");
                    System.out.println();
                    count = 2;                          // count=1
                    Object.class.notifyAll();           // 唤醒全部线程
                }
                // 线程等待一定要放在while循环外
                Object.class.wait();
            }
        }
    }


    public void print2() throws InterruptedException {
        for (int i = 0; i < 100; i++) {
            synchronized (Object.class) {
                while (count == 2) {
                    Thread.sleep(10);
                    System.out.print("虽");
                    System.out.print("远");
                    System.out.print("必");
                    System.out.print("诛");
                    System.out.println();
                    count = 3;
                    Object.class.notifyAll();
                }
                Object.class.wait();
            }
        }
    }

    public void print3() throws InterruptedException {
        for (int i = 0; i < 100; i++) {
            synchronized (Object.class) {
                while (count == 3) {
                    Thread.sleep(10);
                    System.out.print("我");
                    System.out.print("是");
                    System.out.print("中");
                    System.out.print("国");
                    System.out.print("人");
                    System.out.println();
                    count = 1;
                    Object.class.notifyAll();              //唤醒该锁对应的全部线程
                }
                Object.class.wait();
            }
        }
    }
}
